# -*-coding:utf-8-*-
# ProjectName: smalleel-api-tools
# FileName: server_fee.py
# @Time :2023/4/21 16:41
# @Author :
# @Description:服务费计算

from common.util.jsonUtil.jsonUtil import read_sql_json, read_extract_json
from common.util.mySqlUtil.mySqlUtil import select_sql
from common.util.requestUtil.requestUtil import RequestsUtil
from tool.process.config.smalleelApi import *
from tool.process.smalleelLogin import Login
from tool.process.config.acceptConfig import AcceptanceConfig


class ServerFee:
    def __init__(self):
        self.config = AcceptanceConfig()
        self.Smalleelapi = SmalleelApi()
        self.PlatformApi = PlatformApi()
        self.req = RequestsUtil()
        self.smalleel_token = read_extract_json('tokenPro')
        self.platform_token = read_extract_json('tokenPlatform', 6)

    def switchorg(self):
        """
        物业切换项目
        """
        Login().login_smalleel()
        url = self.Smalleelapi.switchSmalleelPro
        headers = {"Authorization": read_extract_json('tokenPro')}
        params = {"type": 5,
                  "aid": self.config.proId}
        response = self.req.send_request("post", url=url, params=params, headers=headers,
                                         parameterFormat="data")
        if response["code"] == 200:
            print(f"已切换到: 【{self.config.proName}】项目")
        else:
            print("项目切换失败！")

    def device_srvfee(self):
        """
        平台设备对应的服务费
        """
        url = self.PlatformApi.devicesrvfee
        headers = {"Token": self.platform_token}
        params = {
            "proid": self.config.proId
        }
        response = self.req.send_request("post", url, params=params, headers=headers,
                                         parameterFormat="data")
        if response["code"] == 200:
            print(f"------平台端：【{self.config.proName}】项目------")
            for i in range(len(response['data'])):
                deviceAid02 = str(response['data'][i]['aid'])
                deviceName = select_sql(read_sql_json('acceptance_sql', 'deviceAid01', 1, deviceAid02))[0][0]
                print(f"已验收设备类型：{deviceName},",
                      f"设备发货数：{response['data'][i]['sendOutNum']}个,",
                      f"设备买断数：{response['data'][i]['buyDevNum']}个,",
                      f"已验收设备数：{response['data'][i]['chargeDevNum']}个,",
                      f"设备服务费：{response['data'][i]['fixedSrvfee']}元")

    def select_stock(self):
        """
        平台查询项目出入库总数

        """
        url = self.PlatformApi.platformstock
        headers = {"Token": self.platform_token}
        params = {
            "start": 1,
            "limit": 50,
            "proid": self.config.proId
        }
        response = self.req.send_request("post", url, params=params, headers=headers,
                                         parameterFormat="data")
        print(f"------平台端：【{self.config.proName}】项目------")
        for i in range(int(response['data']['total'])):
            print(f"{response['data']['records'][i]['name']}:", f" {response['data']['records'][i]['modelName']}, ",
                  f"入库：{response['data']['records'][i]['inNum']}个,", f"出库：{response['data']['records'][i]['outNum']}个,",
                  f"留存数：{response['data']['records'][i]['outNum'] - response['data']['records'][i]['inNum']}个,",
                  f"上线数：{response['data']['records'][i]['onlineNum']}个")

    def notcheck_acceptlist(self):
        """
        平台查询项目未验收数

        """
        url = self.PlatformApi.notCheckList
        headers = {"Token": self.platform_token}
        params = {
            "start": 1,
            "limit": 50,
            "proId": self.config.proId
        }
        response = self.req.send_request("post", url, params=params, headers=headers,
                                         parameterFormat="data")
        if response["code"] == 200 and int(response["data"]["total"]) != 0:
            print(f"------平台端：【{self.config.proName}】项目------")
            print(f"验收数：{response['data']['list'][0]['checkAcceptNum']}个,",
                  f"备用数：{response['data']['list'][0]['reserveNum']}个,",
                  f"上线数：{response['data']['list'][0]['onlineNum']}个,",
                  f"留存数：{response['data']['list'][0]['retainedNum']}个,",
                  f"退回数：{response['data']['list'][0]['returnNum']}个,",
                  f"发货数：{response['data']['list'][0]['sendNum']}个,",
                  f"未验收数：{response['data']['list'][0]['unCheckNum']}个")
        else:
            print(f"【{self.config.proName}】项目,无未验收数据！")

    def accept_sum(self):
        """
        物业查询项目验收记录

        """
        url = self.Smalleelapi.acceptRecordSum
        headers = {"Authorization": self.smalleel_token}
        params = {
            "start": 1,
            "limit": 50,
            "proId": self.config.proId
        }
        response = self.req.send_request("post", url, params=params, headers=headers)
        print(f"------物业端：【{self.config.proName}】项目------")
        print(f"发货数：{response['data']['sendNum']}个,", f"退货数：{response['data']['returnNum']}个,",
              f"留存数：{response['data']['retainedNum']}个,", f"未验收数：{response['data']['unCheckNum']}个,",
              f"待确认数：{response['data']['preProNum']}个,", f"服务费验收数：{response['data']['checkAcceptNum']}个,",
              f"服务费备用数：{response['data']['checkAcceptBackupNum']}个,", f"买断验收数：{response['data']['checkBuyNum']}个,",
              f"买断备用数：{response['data']['checkBuyBackupNum']}个")

    def bill_detail(self, month='202304'):
        """
        物业账单明细，month传对应月份字符串
        """
        # self.switchorg()
        url = self.Smalleelapi.billdetail
        headers = {"Authorization": self.smalleel_token}
        params = {
            "proid": self.config.proId,
            "zwyf": month,
            "start": 1,
            "limit": 50
        }
        response = self.req.send_request("post", url, params=params, headers=headers,
                                         parameterFormat="data")
        if response["code"] == 200 and int(response["data"]["total"]) != 0:
            print(f"------物业端：【{self.config.proName}】项目------")
            for i in range(int(response['data']['total'])):
                print(f"设备名：{response['data']['list'][i]['name']},"
                      f"设备数量：{response['data']['list'][i]['devNum']}个,"
                      f"服务费总金额：{response['data']['list'][i]['srvfee']}元")
        else:
            print("该月暂无服务费，请重新输入月份！")


if __name__ == '__main__':
    Login().login_smalleel()
    sf = ServerFee()
    # sf.device_srvfee()
    # sf.select_stock()
    # sf.accept_sum()
    sf.notcheck_acceptlist()
    # sf.bill_detail(month='202304')
    # sf.switchorg()
